﻿using System;

namespace YuFeng.Web.Helpers
{
    public static class ConfigHelper
    {
        public static string Get(string key)
        {
            return GetAppSetting(key);
        }
        public static int? GetInt(string key)
        {
            int re = 0;
            string value = GetAppSetting(key);
            if (String.IsNullOrEmpty(value))
                return null;
            int.TryParse(value.Trim(), out re);
            return re;
        }
        public static decimal? GetDecimal(string key)
        {
            decimal re = 0;
            string value = GetAppSetting(key);
            if (String.IsNullOrEmpty(value))
                return null;
            decimal.TryParse(GetAppSetting(key).Trim(), out re);
            return re;
        }
        public static bool GetBool(string key)
        {
            try
            {
                var re = GetAppSetting(key);
                if (re.Equals("true", StringComparison.OrdinalIgnoreCase)
                    || re.Equals("1", StringComparison.OrdinalIgnoreCase))
                    return true;
                return false;
            }
            catch
            {
                return false;
            }
        }
        private static string GetAppSetting(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }
    }
}
